//
//  File.swift
//  
//
//  Created by safiri on 2020/8/10.
//

import UIKit

/**
    输入限制类型

    - None:                 什么都不限制
    - OnlyNumber:           只允许输入数字 并限制输入数字的最大位数
    - OnlyNumberExceptLeadingZero: 只允许输入数字 且首位不为零 并限制输入的最大位数
    - TelephoneNumber:      电话号码 默认限制11位
    - DecimalNumber:        小数 分别限制整数部分和小数部分位数
    - DecimalNumberExceptLeadingZero:输入小数 且首位不为零 分别限制整数部分和小数部分位数
    - IDCardNumber:         身份证件号码
    - MaxCharacter:         只限制最大数
    - Express:              自定义的正则表达式
*/
public enum LimitTextFieldInputRegularType {
    case RegularNone
    case RegularOnlyNumber(Int)
    case RegularOnlyNumberExceptLeadingZero(Int)
    case RegularTelephoneNumber
    case RegularDecimalNumber(Int,Int)
    case RegularDecimalNumberExceptLeadingZero(Int,Int)
    case RegularIDCardNumber
    case RegularMaxCharacter(Int)
    case RegularExpress(String)
    case RegularChinese(Int)
    case RegularNumbersLetters(Int)
    case RegularPasswordStyle1(Int)
    case RegularNoSpace
    case RegularChineseNumbersLetters(Int)
    
    /// 正则表达式
    /// - Returns: 正则表达式字符串
    func regularExpression() -> String{
        var expression:String = "^.*$"
        
        switch self {
            
        case .RegularTelephoneNumber:
            expression = "^[0-9]{0,11}$"
        case .RegularDecimalNumber(let integerMaxCount, let decimalMaxCount):
            if integerMaxCount == Int.max && decimalMaxCount == Int.max{
                expression = "(^[0]|^[1-9][0-9]*)(\\.[0-9]*)?$"
            }else if integerMaxCount == Int.max{
                expression = "(^[0]|^[1-9][0-9]*)(\\.[0-9]{0,\(decimalMaxCount)})?$"
            }else if decimalMaxCount == Int.max{
                expression = "(^[0]|^[1-9][0-9]{0,\(integerMaxCount-1)})(\\.[0-9]*)?$"
            }else{
                expression = "(^[0]|^[1-9][0-9]{0,\(integerMaxCount-1)})(\\.[0-9]{0,\(decimalMaxCount)})?$"
            }
        case .RegularDecimalNumberExceptLeadingZero(let integerMaxCount, let decimalMaxCount):
            if integerMaxCount == Int.max && decimalMaxCount == Int.max{
                expression = "(^[1-9][0-9]*)(\\.[0-9]*)?$"
            }else if integerMaxCount == Int.max{
                expression = "(^[1-9][0-9]*)(\\.[0-9]{0,\(decimalMaxCount)})?$"
            }else if decimalMaxCount == Int.max{
                expression = "(^[1-9][0-9]{0,\(integerMaxCount-1)})(\\.[0-9]*)?$"
            }else{
                expression = "(^[1-9][0-9]{0,\(integerMaxCount-1)})(\\.[0-9]{0,\(decimalMaxCount)})?$"
            }
            
        case .RegularIDCardNumber:
            expression = "^[0-9]{0,17}[0-9Xx]$"
            
        case .RegularMaxCharacter(let characterMaxCount):
            if characterMaxCount == Int.max{
                expression = "^.*$"
            }else{
                expression = "^.{0,\(characterMaxCount)}$"
            }
            
        case .RegularOnlyNumber(let integerMaxCount):
            if integerMaxCount == Int.max{
                expression = "^\\d*$"
            }else{
                expression = "^\\d{0,\(integerMaxCount)}$"
            }
            
        case .RegularOnlyNumberExceptLeadingZero(let integerMaxCount):
            if integerMaxCount == Int.max{
                expression = "^[1-9][0-9]*$"
            }else{
                expression = "^[1-9][0-9]{0,\(integerMaxCount-1)}$"
            }
            
        case .RegularChinese(let integerMaxCount):
            if integerMaxCount == Int.max{
                expression = "^[\\u4E00-\\u9FA5]*$"
            }else{
                expression = "^[\\u4E00-\\u9FA5]{0,\(integerMaxCount)}$"
            }
        case .RegularNumbersLetters(let integerMaxCount):
            if integerMaxCount == Int.max{
                expression = "^[A-Za-z0-9]*$"
            }else{
                expression = "^[A-Za-z0-9]{0,\(integerMaxCount)}$"
            }
        case .RegularPasswordStyle1(let integerMaxCount):
            if integerMaxCount == Int.max{
                expression = "^[a-zA-Z0-9-*/+.~!@#$%^&*()]*$"
            }else{
                expression = "^[a-zA-Z0-9-*/+.~!@#$%^&*()]{0,\(integerMaxCount)}$"
            }
        case .RegularNoSpace:
            /**
             \S 匹配任意不是空白符的字符
             + 重复一次或更多次
            */
            expression = "\\S+"
        case .RegularChineseNumbersLetters(let integerMaxCount):
            if integerMaxCount == Int.max{
                expression = "^[a-zA-Z0-9\\u4E00-\\u9FA5]*$"
            }else{
                expression = "^[a-zA-Z0-9\\u4E00-\\u9FA5]{0,\(integerMaxCount)}$"
            }
        case .RegularExpress(let exp):
            expression = exp
        default:
            break
        }
        return expression
    }
    
    func keyboardType() -> UIKeyboardType {
        var keyboardType = UIKeyboardType.default
        switch self {
        case .RegularOnlyNumberExceptLeadingZero(_),.RegularOnlyNumber(_):
            keyboardType = .numberPad
        case .RegularTelephoneNumber:
            keyboardType = .phonePad
        case .RegularDecimalNumber(_, _), .RegularDecimalNumberExceptLeadingZero(_, _):
            keyboardType = .decimalPad
        default:
            keyboardType = .default
        }
        return keyboardType
    }
}
